import 'package:flutter/material.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/widget/w_text.dart';

class CustomSearch extends StatefulWidget {
  final String? hintText;
  final String? initText;
  final double height;
  final double horizontalMargin;
  final double verticalMargin;
  final Color? sortColor;
  final Function(String text)? onTextChange;
  final Function? onSortClick;
  final bool showSort;
  final bool showSearch;

  const CustomSearch(
      {super.key,
      this.hintText = "搜索",
      this.initText = "",
      this.height = 40,
      this.horizontalMargin = 15,
      this.verticalMargin = 10,
      this.sortColor,
      this.showSort = true,
      this.showSearch = true,
      this.onTextChange,
      this.onSortClick});

  @override
  State<CustomSearch> createState() => _CustomSearchState();
}

class _CustomSearchState extends State<CustomSearch> {
  final TextEditingController _searchController =
      TextEditingController.fromValue(const TextEditingValue(text: ""));
  bool clearShow = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _searchController.text = widget.initText ?? "";
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: WColors.white,
      child: Container(
        height: widget.height.px,
        margin: EdgeInsets.symmetric(
          horizontal: widget.horizontalMargin.px,
          vertical: widget.verticalMargin.px,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (widget.showSearch)
              Expanded(
                  child: Container(
                //  height: widget.height,
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: 10.px),
                decoration: BoxDecoration(
                    // boxShadow: [
                    //   BoxShadow(
                    //       color: WColors.CDFEFFB,
                    //       offset: const Offset(0, 1),
                    //       blurRadius: 3.px)
                    // ],
                    color: WColors.CF8F9FA,
                    borderRadius: BorderRadius.circular(30.px)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Icon(
                      Icons.search,
                      color: WColors.C8A8A8A,
                    ),
                    SizedBox(
                      width: 5.px,
                    ),
                    Expanded(
                        child: TextField(
                      keyboardAppearance: Brightness.light,
                      autofocus: false,
                      controller: _searchController,
                      onChanged: (s) {
                        clearShow = s.isNotEmpty ? true : false;
                        setState(() {});
                        if (widget.onTextChange != null) {
                          widget.onTextChange!(s);
                        }
                      },
                      style: TextStyle(
                          color: WColors.mainTextColor, fontSize: 14.px),
                      keyboardType: TextInputType.text,
                      decoration: InputDecoration(
                        focusedBorder: const OutlineInputBorder(
                            borderSide: BorderSide(
                                width: 0, color: Colors.transparent)),
                        disabledBorder: const OutlineInputBorder(
                            borderSide: BorderSide(
                                width: 0, color: Colors.transparent)),
                        enabledBorder: const OutlineInputBorder(
                            borderSide: BorderSide(
                                width: 0, color: Colors.transparent)),
                        border: const OutlineInputBorder(
                            borderSide: BorderSide(
                                width: 0, color: Colors.transparent)),
                        contentPadding: EdgeInsets.zero,
                        counterText: "",
                        hintText: widget.hintText,
                        hintStyle:
                            TextStyle(color: WColors.CCCCCCC, fontSize: 14.px),
                      ),
                    )),
                    Offstage(
                        offstage: !clearShow,
                        child: GestureDetector(
                            onTap: () {
                              _searchController.clear();
                              clearShow = false;
                              if (widget.onTextChange != null) {
                                widget.onTextChange!("");
                              }
                              setState(() {});
                            },
                            child: Image.asset('images/ic_del_grey.png',
                                width: 16.px)))
                  ],
                ),
              )),
            if (!widget.showSearch)
              Expanded(child: Container()),
            Offstage(
              offstage: !widget.showSort,
              child: GestureDetector(
                onTap: () {
                  if (widget.onSortClick != null) {
                    widget.onSortClick!();
                  }
                },
                child: Container(
                  height: widget.height.px - 2.px,
                  margin: EdgeInsets.only(left: 10.px),
                  padding: EdgeInsets.symmetric(horizontal: 10.px),
                  decoration: BoxDecoration(
                      color: WColors.CF8F9FA,
                      //   border: Border.all(color: WColors.border,width: 1.px),
                      borderRadius: BorderRadius.circular(widget.height.px)),
                  child: Row(
                    children: [
                      Image.asset(
                        "images/ic_sort.png",
                        width: 20.px,
                        color: widget.sortColor ?? WColors.mainTextColor,
                      ),
                      SizedBox(width: 8.px),
                      WText(
                        "筛选",
                        style: TextStyle(
                            color: widget.sortColor ?? WColors.mainTextColor,
                            fontSize: 14.px),
                      )
                    ],
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
